#coding: utf-8
class CulturalHeritage::Incidence < ActiveRecord::Base
 #default_scope where("cultural_heritage_incidences.deleted = 0")
  #has_many :list_avatars, :class_name => 'CulturalHeritage::IncidencesAvatar',  :foreign_key => 'incidence'
  #has_many :list_incidences, :class_name => 'CulturalHeritage::Incidence',  :foreign_key => 'foundInId'
  belongs_to :obj_cultural_heritage, :class_name => 'CulturalHeritage::CulturalHeritage', :foreign_key => 'culturalHeritage'
  belongs_to :obj_user, :class_name => 'User', :foreign_key => 'user'
  belongs_to :obj_person1, :class_name => 'Security::Person', :foreign_key => 'person1'
  belongs_to :obj_person2, :class_name => 'Security::Person', :foreign_key => 'person2'
  belongs_to :obj_person3, :class_name => 'Security::Person', :foreign_key => 'person3'
  has_attached_file :avatar
  acts_as_gmappable :lat => 'latitude', :lng =>'longitude',:process_geocoding => :geocode?
#    class User < ActiveRecord::Base
#      has_attached_file :avatar
#    end
  def geocode?
    !(address.blank? || (!latitude.blank? && !longitude.blank?))
  end

  def gmaps4rails_address
  #describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
    "#{self.address}"
  end
  
  default_scope where("deleted = 0")

  

  def str_state
    if state == 0
      return "Registrado"
    end
    if state == 1
      return "Atendido"
    end
    if state == 2
      return "Rechazado"
    end
    if state == 3
      return "Asignado_INC"
    end
    if state == 4
      return "Asignado_Experto"
    end
    if state == 5
      return "Asignado_Experto_Ley"
    end
  end

  def self.count_user_dependencies(id)
      return find(:all,:conditions=>["cultural_heritage_incidences.user = ? and (cultural_heritage_incidences.deleted = ?) ",id,0],:select=>"count(*) as 'incidences'")[0].incidences.to_i
  end

  def self.count_cultural_heritage_dependencies(id)
      return find(:all,:conditions=>["cultural_heritage_incidences.culturalHeritage = ? and (cultural_heritage_incidences.deleted = ?) ",id,0],:select=>"count(*) as 'incidences'")[0].incidences.to_i
  end

  def self.count_department_dependencies(id)
      return find(:all,:conditions=>["cultural_heritage_incidences.department = ? and (cultural_heritage_incidences.deleted = ?) ",id,0],:select=>"count(*) as 'incidences'")[0].incidences.to_i
  end

  def self.count_province_dependencies(id)
      return find(:all,:conditions=>["cultural_heritage_incidences.province = ? and (cultural_heritage_incidences.deleted = ?) ",id,0],:select=>"count(*) as 'incidences'")[0].incidences.to_i
  end

  def self.count_district_dependencies(id)
      return find(:all,:conditions=>["cultural_heritage_incidences.district = ? and (cultural_heritage_incidences.deleted = ?) ",id,0],:select=>"count(*) as 'incidences'")[0].incidences.to_i
  end

  def self.count_person_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_incidences.person1 = ? or cultural_heritage_incidences.person2 = ?  or cultural_heritage_incidences.person3 = ? ) and (cultural_heritage_incidences.deleted = ?) ",id,id,id,0],:select=>"count(*) as 'incidences'")[0].incidences.to_i
  end

  def erasable
    ava = CulturalHeritage::IncidencesAvatar.count_incidence_dependencies(id)
    if ava >0
      return "Imposible borrar. Existen dependencias en imagenes de patrimonio cultural"          
    end
    return nil
  end
end
